package com.mall.server.controller;

import com.mall.commons.R;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.server.service.ProductAttributeService;
import com.tanhua.model.domain.PmsProductAttribute;
import com.tanhua.model.dto.fuDto.PageDto;
import com.tanhua.model.vo.ProductAttributeProductVo;
import com.tanhua.model.vo.PageVo;
import com.tanhua.model.vo.ProductAttributeCategoryListWithAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/productAttribute")
public class ProductAttributeController {
    @Autowired
    private ProductAttributeService productAttributeService;




    /**
     * 根据分类查询属性列表或参数列表
     * @param cid  传来的cid 就是id
     * @param type
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("list/{cid}")
    public R findTypeList(@PathVariable("cid") Integer cid, Integer type,
                          @RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "5") Integer pageSize ){

        if (cid == null || type ==null) {
            throw new BusinessException(ErrorResult.emptyError());
        }
        PageVo pageVo =productAttributeService.findTypeList(cid,type,pageNum,pageSize);


        return R.success(pageVo);
    }

    /**
     * 查询单个商品属性
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R productAttribute(@PathVariable("id") Integer id){
        if (id ==null) {
            throw new BusinessException(ErrorResult.emptyError());
        }
        PmsProductAttribute pmsProductAttribute=productAttributeService.findById(id);
        return R.success(pmsProductAttribute);
    }

    /**
     * 修改商品属性信息
     * @param id
     * @param pmsProductAttribute
     * @return
     */
    @PostMapping("/update/{id}")
    public R updateById(@PathVariable("id")Long id,@RequestBody PmsProductAttribute pmsProductAttribute){
        if (id == null || pmsProductAttribute ==null){
            throw new BusinessException(ErrorResult.emptyError());
        }
        pmsProductAttribute.setId(id);
        productAttributeService.updateById(pmsProductAttribute);
        return R.success("");
    }

    /**
     * 添加商品属性信息
     * @param pmsProductAttribute
     * @return
     */
    @PostMapping("/create")
    public R categoryCreate(@RequestBody PmsProductAttribute pmsProductAttribute){
        if ( pmsProductAttribute  ==null){
            throw new BusinessException(ErrorResult.emptyError());
        }
        productAttributeService.save(pmsProductAttribute);

        return R.success("");
    }

    /**
     * 批量删除商品属性
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public R deleteByIds(Integer[] ids){
        productAttributeService.deleteByIds(ids);
        return R.success("");
    }





    /**
     * 分页获取所有商品属性分类
     * @param pageDto
     * @return
     */
    @GetMapping("/category/list")
    public R categoryList(PageDto pageDto){
        PageVo pageVo = productAttributeService.categoryList(pageDto);
        return R.success(pageVo);
    }

    /**
     * 添加商品属性分类
     * @return
     */
    @PostMapping("/category/create")
    public R categoryCreate(String name){
        productAttributeService.categorySave(name);
        return R.success("");
    }

    /**
     * 修改商品属性分类
     * @param id
     * @param name
     * @return
     */
    @PostMapping("/category/update/{id}")
    public R categoryUpdate(@PathVariable("id") Long id,String name){
        if ( id  ==null || name==null){
            throw new BusinessException(ErrorResult.emptyError());
        }
        productAttributeService.categoryUpdate(id,name);
        return R.success("");
    }

    /**
     * 删除单个商品属性分类
     * @param id
     * @return
     */
    @GetMapping("/category/delete/{id}")
    public R categoryDelete(@PathVariable("id") Long id){
        productAttributeService.categoryDelete(id);
        return R.success("");
    }


    /**
     * 获取所有商品属性分类及其下属性
     * @return
     */
    @GetMapping("/category/list/withAttr")
    public R categoryListWithAttr(){
        List<ProductAttributeCategoryListWithAttrVo> list=productAttributeService.categoryListWithAttr();
        return R.success(list);
    }


    /**
     * 根据productCategoryId,获得attributeCategoryId,attributeId
     * @param productCategoryId
     * @return
     */
    @GetMapping("/attrInfo/{productCategoryId}")
    public R attrInfoProductCategoryId(@PathVariable("productCategoryId")Long productCategoryId){
        List<ProductAttributeProductVo> productAttributeProductVo =productAttributeService.attrInfoProductCategoryId(productCategoryId);
        return R.success(productAttributeProductVo);
    }



}
